<script setup>

import LightCard from "@/componets/LightCard.vue";
import {
    Calendar,
    CircleCheck,
    Clock,
    Collection,
    CollectionTag,
    EditPen,
    FolderAdd, FolderChecked,
    Star
} from "@element-plus/icons-vue";
import Weather from "@/componets/Weather.vue";
import {get} from "@/net/index.js";
import {ElMessage} from "element-plus";
import TopicEditor from "@/componets/TopicEditor.vue";
import {computed, reactive, ref, watch} from "vue";
import {useStore} from "@/store/index.js";
import axios from "axios";
import ColorDot from "@/componets/ColorDot.vue";
import router from "@/router/index.js";
import TopicCollectList from "@/componets/TopicCollectList.vue";

const today = computed(() => {
    const date = new Date();
    return `${date.getFullYear()}年${date.getMonth() + 1}月${date.getDate()}日`;
})
const collects = ref(false)
const weather = reactive({
    location: {},
    now: {},
    hourly: [],
    success: false
})

navigator.geolocation.getCurrentPosition(position => {
    const longitude = position.coords.longitude;
    const latitude = position.coords.latitude;
    get(`api/forum/weather?longitude=${longitude}&latitude=${latitude}`, data => {
        Object.assign(weather, data);
        weather.success = true
    })

}, error => {
    console.log(error)
    ElMessage.warning('获取天气信息失败')
    get(`api/forum/weather?longitude=116.40529&latitude=39.90499`, data => {
        Object.assign(weather, data);
        weather.success = true
    })
}, {
    timeout: 3000,
    enableHighAccuracy: true
})

const store = useStore()
const editor = ref(false)

const topics = reactive({
    list: [],
    type: 0,
    page: 0,
    end: false,
    top: []
})
watch(() => topics.type, () => resetList(), {immediate: true})

function updateList() {
    if (topics.end) {
        return
    }
    get(`api/forum/list-topic?page=${topics.page}&type=${topics.type}`, data => {
        if (data) {
            data.forEach(d => topics.list.push(d))
            topics.page++
        }
        if (!data || data.length < 10) {
            topics.end = true
        }
    })
}

get('api/forum/top-topic', data => topics.top = data)

function onTopicCreate() {
    editor.value = false
    resetList()
}

function resetList() {
    topics.page = 0;
    topics.end = false
    topics.list = []
    updateList()
}

</script>

<template>
    <div style="display: flex; margin: 20px auto; gap: 20px; max-width: 1000px">
        <div style="flex: 1">
            <light-card @click="editor=true">
                <div class="create-topic">
                    <el-icon>
                        <EditPen/>
                    </el-icon>
                    点击发布主题
                </div>
            </light-card>


            <light-card style="margin-top: 10px;display: flex;flex-direction: column;gap: 10px">
                <div v-for="item in topics.top" class="top-topic"
                     @click="router.push(`/index/topic-detail/${item.id}`)">
                    <el-tag size="small" type="info">置顶</el-tag>
                    <div>{{ item.title }}</div>
                    <div>{{ new Date(item.time).toLocaleDateString() }}</div>
                </div>
            </light-card>

            <!--      指定帖子-->
            <light-card style="margin-top: 10px; display: flex; gap: 7px">
                <div v-for="item in store.forum.types"
                     :class="`type-select-card ${topics.type === item.id ? 'active' : ''}`"
                     @click="topics.type = item.id">
                    <color-dot :color="item.color"/>
                    <span style="margin-left: 5px">{{ item.name }}</span>
                </div>
            </light-card>
            <transition mode="out-in" name="ed-fade-in">
                <div v-if="topics.list?.length">
                    <div v-infinite-scroll="updateList"
                         style="margin-top: 10px; display: flex; flex-direction: column; gap: 10px">

                        <light-card v-for="item in topics.list" class="topic-card"
                                    @click="router.push('/index/topic-detail/'+item.id)">
                            <div style="display: flex; margin-bottom: 8px">
                                <div>
                                    <el-avatar :size="30" :src="`${axios.defaults.baseURL}/images${item.avatar}`"/>
                                </div>
                                <div style="margin-left: 7px">
                                    <div style="font-size: 13px; font-weight: bold">{{ item.username }}</div>
                                    <div style="font-size: 13px; color: grey">
                                        <el-icon>
                                            <Clock/>
                                        </el-icon>
                                        <div
                                            style="margin-left: 2px; display: inline-block; transform: translateY(-2px)">
                                            {{ new Date(item.time).toLocaleString() }}
                                        </div>
                                    </div>
                                </div>
                            </div>
                            <div>
                                <div :style="{color: store.findTypeById(item.type).color + 'EE',
                                             'border-color': store.findTypeById(item.type).color + 'DD',
                                             'background': store.findTypeById(item.type).color + '33'    }" class="topic-type">
                                    {{ store.findTypeById(item.type).name }}
                                </div>
                                <span style="font-weight: bold; margin-left: 6px">
              {{ item.title }}
            </span>
                            </div>
                            <div class="topic-content">
                                {{ item.text }}
                            </div>
                            <div style="display: grid;grid-template-columns: repeat(3, 1fr);grid-gap: 10px">
                                <el-image v-for="img in item.images" :src="img" class="topic-image"
                                          fit="cover"></el-image>
                            </div>
                            <div style="display: flex;gap: 20px;font-size: 13px;margin-top: 10px;opacity: 0.8">
                                <div>
                                    <el-icon style="vertical-align: middle">
                                        <CircleCheck/>
                                    </el-icon>
                                    {{ item.like }}点赞
                                </div>
                                <div>
                                    <el-icon style="vertical-align: middle">
                                        <Star/>
                                    </el-icon>
                                    {{ item.collect }}收藏
                                </div>
                            </div>
                        </light-card>

                    </div>

                </div>

            </transition>

        </div>

        <div style="width: 280px">
            <div style="position: sticky; top: 20px;">

                <light-card>
                    <div class="collect-list-button" @click="collects = true">
                        <span><el-icon><FolderChecked/></el-icon> 查看我的收藏</span>
                        <el-icon style="transform: translateY(3px)">
                            <ArrowRightBold/>
                        </el-icon>
                    </div>
                </light-card>

                <light-card style="margin-top: 10px">
                    <div style="font-weight: bold">
                        <el-icon>
                            <CollectionTag/>
                        </el-icon>
                        论坛公告
                    </div>
                    <el-divider style="margin: 10px 0"></el-divider>
                    <div style="font-size: 14px; margin: 10px; color: grey">
                        这是论坛公告内容
                    </div>
                </light-card>

                <light-card style="margin-top: 10px">
                    <div style="font-weight: bold">
                        <el-icon>
                            <Calendar/>
                        </el-icon>
                        天气信息
                    </div>
                    <el-divider style="margin: 10px 0"></el-divider>
                    <Weather :data="weather"/>
                </light-card>

                <light-card style="margin-top: 10px">
                    <div class="info-text">
                        <div>当前日期</div>
                        <div>{{ today }}</div>
                    </div>
                    <div class="info-text">
                        <div>当前IP地址</div>
                        <div>127.0.0.1</div>
                    </div>
                </light-card>
            </div>
        </div>

        <topic-editor :show="editor" @close="editor = false" @success="onTopicCreate"/>
        <topic-collect-list :show="collects" @close="collects = false"/>
    </div>
</template>

<style lang="less" scoped>

.collect-list-button {
    font-size: 14px;
    display: flex;
    justify-content: space-between;
    transition: .3s;

    &:hover {
        cursor: pointer;
        opacity: 0.6;
    }
}


.top-topic {
    display: flex;

    div:first-of-type {
        font-size: 14px;
        margin-left: 10px;
        font-weight: bold;
        opacity: 0.8;
        transition: color .3s;

        &:hover {
            color: grey;
        }
    }

    div:nth-of-type(2) {
        flex: 1;
        color: grey;
        font-size: 13px;
        text-align: right;
    }

    &:hover {
        cursor: pointer;
    }
}

.type-select-card {
    background-color: #f5f5f5;
    padding: 2px 7px;
    font-size: 14px;
    border-radius: 3px;
    box-sizing: border-box;
    transition: background-color .3s;

    &.active {
        border: solid 1px #ead4c4;
    }

    &:hover {
        cursor: pointer;
        background-color: #dadada;
    }
}

.topic-card {

    padding: 15px;
    transition: scale .3s;

    &:hover {
        scale: 1.015;
        cursor: pointer;
    }

    .topic-content {
        color: grey;
        font-size: 13px;
        margin: 5px 0;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 3;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .topic-type {
        display: inline-block;
        border: solid 0.5px grey;
        border-radius: 5px;
        font-size: 12px;
        padding: 0 5px;
        height: 18px;
    }
}

.info-text {
    display: flex;
    justify-content: space-between;
    color: grey;
    font-size: 14px;
}

.create-topic {
    background-color: #efefef;
    border-radius: 5px;
    height: 40px;
    color: grey;
    line-height: 40px;
    padding: 0 10px;

    &:hover {
        cursor: pointer;
    }
}

.topic-image {
    width: 100%;
    height: 100%;
    max-height: 120px;
}

.dark {
    .create-topic {
        background-color: #232323;
    }

    .type-select-card {
        background-color: #282828;

        &.active {
            border: solid 1px #64594b;
        }

        &:hover {
            background-color: #5e5e5e;
        }
    }

}
</style>